package ex55.mm.flow.impound.quote.pub;

import com.google.common.collect.Maps;
import ex55.mm.flow.impound.quote.AggregateDataCalculator;
import ex55.mm.flow.impound.quote.aggregate.ConvertDepth;
import ex55.mm.flow.impound.quote.aggregate.OnePathRst;
import ex55.mm.flow.impound.quote.aggregate.SimpleConvertDepth;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1")
public class ImpoundFacade {

    @Autowired
    private AggregateDataCalculator aggregateDataCalculator;

    @GetMapping("/conversion/{baseCurrency}/{quoteCurrency}")
    public Map<String, Object> getMarketDataDepth(
            @PathVariable("baseCurrency") String baseCurrency,
            @PathVariable("quoteCurrency") String quoteCurrency,
            @RequestParam(value = "limit", required = false) Integer limit) {
        ConvertDepth marketDataDepth = aggregateDataCalculator.getMarketDataDepth(baseCurrency, quoteCurrency);
        Map<String, Object> result = Maps.newHashMap();

        result.put("from", baseCurrency);
        result.put("to", quoteCurrency);
        result.put("status", "success");

        if (limit == null) {
            limit = 5;
        }

        List<ConvertDepth.Entry> asks = marketDataDepth.getAsks();
        if (!CollectionUtils.isEmpty(asks)) {
            if (asks.size() > limit) {
                result.put("asks", asks.subList(0, limit));
            } else {
                result.put("asks", asks);
            }
        } else {
            result.put("asks", null);
        }

        List<ConvertDepth.Entry> bids = marketDataDepth.getBids();
        if (!CollectionUtils.isEmpty(bids)) {
            if (asks.size() > limit) {
                result.put("bids", bids.subList(0, limit));
            } else {
                result.put("bids", bids);
            }
        } else {
            result.put("bids", null);
        }

        result.put("timestamp", DateTime.now().toDate());

        return result;
    }

    @GetMapping("/conversion/simple/{baseCurrency}/{quoteCurrency}")
    public Map<String, Object> getSimpleMarketDataDepth(
            @PathVariable("baseCurrency") String baseCurrency,
            @PathVariable("quoteCurrency") String quoteCurrency,
            @RequestParam(value = "limit", required = false) Integer limit) {
        SimpleConvertDepth marketDataDepth = aggregateDataCalculator.getSimpleMarketDataDepth(baseCurrency, quoteCurrency);
        Map<String, Object> result = Maps.newHashMap();

        result.put("from", baseCurrency);
        result.put("to", quoteCurrency);
        result.put("status", "success");

        if (limit == null) {
            limit = 5;
        }

        List<SimpleConvertDepth.Entry> asks = marketDataDepth.getAsks();
        if (!CollectionUtils.isEmpty(asks)) {
            if (asks.size() > limit) {
                result.put("asks", asks.subList(0, limit));
            } else {
                result.put("asks", asks);
            }
        } else {
            result.put("asks", null);
        }

        List<SimpleConvertDepth.Entry> bids = marketDataDepth.getBids();
        if (!CollectionUtils.isEmpty(bids)) {
            if (asks.size() > limit) {
                result.put("bids", bids.subList(0, limit));
            } else {
                result.put("bids", bids);
            }
        } else {
            result.put("bids", null);
        }

        result.put("timestamp", DateTime.now().toDate());

        return result;
    }

    @GetMapping("/OnePathRst/{baseCurrency}/{quoteCurrency}")
    public Map<String, OnePathRst> getOnePathRst(
            @PathVariable("baseCurrency") String baseCurrency,
            @PathVariable("quoteCurrency") String quoteCurrency,
            @RequestParam("pathId") String pathId) {
        return aggregateDataCalculator.getOnePathRst(baseCurrency, quoteCurrency, pathId);
    }

    @GetMapping("/OnePathRst/symbol.list")
    public Collection<Map<String, String>> getSymbols() {
        return aggregateDataCalculator.getSymbols();
    }
}
